package servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.io.XMLWriter;

import elements.Constants;
import elements.Deal;
import elements.Parameter;
import elements.User;
import export.db.Loader;
import export.generator.Generator;

/**
 * Servlet implementation class DealsServlet
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private Loader loader;
	private Generator generator;
	
	public UserServlet() {
		super();
		loader = new Loader();
		generator = new Generator();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		String userId = request.getParameter(Constants.PARAMETER_USERID);
		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String message = "user not exist";
		if (userId == null) {
			PrintWriter pw = response.getWriter();
			pw.write(message);
			pw.close();
		}
		else {
			User user = loader.loadUser(userId);
			if (user == null) {
				PrintWriter pw = response.getWriter();
				pw.write(message);
				pw.close();
			} else {
				Document result = generator.getStringUser(user);
				XMLWriter output = new XMLWriter(response.getWriter());
				output.write(result);
				output.close();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
		
		String userId = request.getParameter(Constants.PARAMETER_USERID);
		if (userId == null || userId.isEmpty()) {
			pw.write("userId must be set");
			return;
		}
		String type = request.getParameter(Constants.PARAMETER_TYPE);
		String discountPercent = request.getParameter(Constants.PARAMETER_DISCOUNTPERCENT);
		String cityName = request.getParameter(Constants.PARAMETER_CITYNAME);
		String title = request.getParameter(Constants.PARAMETER_TITLE);
		String price = request.getParameter(Constants.PARAMETER_PRICE);
		String website = request.getParameter(Constants.PARAMETER_WEBSITE);
		String endTime = request.getParameter(Constants.PARAMETER_ENDTIME);
		String members = request.getParameter(Constants.PARAMETER_MEMBERS);
		String distance = request.getParameter(Constants.PARAMETER_DISTANCE);
		loader.updateUser(userId, type, website, title, endTime, cityName, price, discountPercent, members, distance);
		
		pw.write("update success");
	}

}
